/****************************
* Auteur :Tanguy Arnaud
* Date : 2008
*
* Copyright (C) 2008 TANGUY Arnaud
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * ----------------------------------------------------------------------- *
 *																		   *
 *	Modifications for FotoKinect, Spring 2011 by Gianna Badiali,		   *
 *                                                   Gabriel Fernandez,	   *
 *                                                   Kelsey Tripp, and     *
 *                                                   Sam Swarr             *
 *																		   *
 ***************************************************************************/

#include "CPixmap.h"
#include <QImage>
#include <qlabel.h>

CPixmap::CPixmap() {
}

CPixmap::CPixmap(const QString &fileName) : QPixmap(fileName), m_filePath(fileName) {
}

CPixmap::CPixmap(const QImage &image) : QPixmap(QPixmap::fromImage(image)), m_image(image) {
}

CPixmap::CPixmap(const QPixmap &pixmap): QPixmap(pixmap){
}

void CPixmap::addEffect(const PictureEffect & effect) {
    switch (effect.effect) {
        case PictureEffect::ClearEffects:
            clearEffects();
            break;
        case PictureEffect::FlipH:
            toHFlip();
            break;
        case PictureEffect::FlipV:
            toVFlip();
            break;
        case PictureEffect::Crop:
            crop(effect.croppingRect);
            break;
    }
}

QList<PictureEffect> CPixmap::effects() const {
    return m_effects;
}

void CPixmap::clearEffects()
{
    if (!m_image.isNull())
        updateImage(m_image);
    else if (!m_filePath.isEmpty())
        load(m_filePath);
    m_effects.clear();
}

void CPixmap::toHFlip() {
    m_effects.push_back(PictureEffect::FlipH);
    QImage img = this->toImage().mirrored(true, false);
    updateImage(img);
}


void CPixmap::toVFlip() {
    m_effects.push_back(PictureEffect::FlipV);
    QImage img = this->toImage().mirrored(false, true);
    updateImage(img);
}


// prepares the pixmap for cropping by snapping the cropping rect to the pixmap bounds
// (to avoid adding any additional black around the edges if cropping rect > pixmap boundary)
/* Returns true if the cropping rectangle has a width and height, false otherwise */
bool CPixmap::crop(const QRect &croppingRect)
{
    if(croppingRect.isNull())
		return false;

    m_effects.push_back(PictureEffect(PictureEffect::Crop, PictureEffect::Crop, croppingRect));

    QImage img = this->toImage();

	QRect newRect = QRect(croppingRect.x(), croppingRect.y(), croppingRect.width(), croppingRect.height());

	// check for cropping rect past both left and right edge of img
	if (croppingRect.x() < 0 && croppingRect.x() + croppingRect.width() > img.width()) {
		newRect.setX(0);
		newRect.setWidth(img.width());
	}
	else {
		// check for cropping rect past left edge of img
		if (croppingRect.x() < 0) {
			newRect.setX(0);
			newRect.setWidth(croppingRect.x() + croppingRect.width());
		}
		// check for cropping rect past right edge of img
		if (croppingRect.x() + croppingRect.width() > img.width()) {
			newRect.setX(croppingRect.x());
			newRect.setWidth(img.width() - croppingRect.x());
		}
	}

	// check for cropping rect past both top and bottom of img
	if (croppingRect.y() < 0 && croppingRect.y() + croppingRect.height() > img.height()) {
		newRect.setY(0);
		newRect.setHeight(img.height());
	}
	else {
		// check for cropping rect past top of img
		if (croppingRect.y() < 0) {
			newRect.setY(0);
			newRect.setHeight(croppingRect.y() + croppingRect.height());
		}
		// check for cropping rect past bottom of img
		if (croppingRect.y() + croppingRect.height() > img.height()) {
			newRect.setY(croppingRect.y());
			newRect.setHeight(img.height() - croppingRect.y());
		}
	}


	if (newRect.width() > 0 && newRect.height() > 0) {
		img = img.copy(newRect);
		updateImage(img);
		return true;
	}
	else{
		return false;
	}
	
}


// updates the image stored
void CPixmap::updateImage(QImage &newImage)
{
    QImage copyImage = m_image;
    QString copyFilePath = m_filePath;
    QList<PictureEffect> copyEffects = m_effects;
    *this = fromImage(newImage);
    m_image = copyImage;
    m_filePath = copyFilePath;
    m_effects = copyEffects;
}
